package com.dfrz.bookstore.service.impl;

import com.dfrz.bookstore.dao.impl.BookDaoImpl;
import com.dfrz.bookstore.dao.impl.BookTypeDaoImpl;
import com.dfrz.bookstore.entry.BookType;
import com.dfrz.bookstore.service.BookTypeService;

import javax.swing.*;
import java.sql.SQLException;
import java.util.List;

/**
 * 类功能描述:
 *
 * @Author keyan
 * @Date 2021/5/20 20:15
 */
public class BookTypeServiceImpl implements BookTypeService {
    BookTypeDaoImpl bookTypeDao = new BookTypeDaoImpl();
    @Override
    public void inertBookType(BookType bookType) {
        /*先查找数据库中是否存在待添加的图书类名*/
        List<BookType> bookTypes = bookTypeDao.findObjByObj(bookType);
        System.out.println(bookTypes.size());
        if (bookTypes.size()==0){
            boolean flag = bookTypeDao.insert(bookType);
            if (flag){
                JOptionPane.showMessageDialog(null,bookType.getType()+"添加成功");
            }else {
                JOptionPane.showMessageDialog(null,bookType.getType()+"添加失败");
            }
        }else {
            JOptionPane.showMessageDialog(null,bookType.getType()+"类别已存在");
        }

    }

    @Override
    public void delectBookTypeById(int id) {

        BookType bookType = new BookType();
        bookType.setId(id);
        List<BookType> bookTypes = bookTypeDao.findObjByObj(bookType);
        if (bookTypes!=null){
            boolean b = bookTypeDao.deleteObjById(id);
            if (b){
                JOptionPane.showMessageDialog(null,"删除成功");
            }else {
                JOptionPane.showMessageDialog(null,"删除失败");
            }

        }else {
            JOptionPane.showMessageDialog(null,"未找到需要删除的图书类别");
        }
    }

    @Override
    public void upDataBookType(BookType bookType) {
        boolean flag = bookTypeDao.updateByObj(bookType);
        if (flag){
            JOptionPane.showMessageDialog(null,"更新成功");
        }else{
            JOptionPane.showMessageDialog(null,"更新失败");
        }
    }

    @Override
    public List<BookType> findAllBookType() {
        BookType bookType = new BookType();
        List<BookType> bookTypes = bookTypeDao.findObjByObj(bookType);
        return bookTypes;
    }
    public int findTypeIDByType(String typeName){
        BookType bookType = new BookType();
        bookType.setType(typeName);
        List<BookType> typeList = bookTypeDao.findObjByObj(bookType);
        if (typeList.size()<=0){
            JOptionPane.showMessageDialog(null,"图书类找不到");
        }

        return typeList.get(0).getId();
    }
}
